﻿using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Core.Infrastructure;
using PMS.Data.Entities.Customers;
using System.Collections.Generic;
using System.Linq;

namespace PMS.Services.EntityServices.Customers
{
    public static class CustomerExtensions
    {
        /// <summary>
        /// 获取当前用户是否是超级管理员(后台管理系统对超级管理员拥有所有权限)
        /// </summary>
        /// <param name="curCustomer"></param>
        /// <param name="customerService"></param>
        /// <returns></returns>
        public static bool IsAdministrator(this Customer curCustomer, ICustomerService customerService = null)
        {
            if (curCustomer == null) return false;
            if(customerService == null)
                customerService = EngineContext.Current.Resolve<ICustomerService>();
            return customerService.IsAdmin(curCustomer);
        }

        /// <summary>
        /// 获取当前用户是否是后台自动执行任务账号
        /// </summary>
        /// <param name="cuCustomer"></param>
        /// <param name="customerService"></param>
        /// <returns></returns>
        public static bool IsBackgroundBuidInTaskUser(this Customer curCustomer, ICustomerService customerService = null)
        {
            if (curCustomer == null) return false;
            if (customerService == null)
                customerService = EngineContext.Current.Resolve<ICustomerService>();
            return customerService.IsBuildInTaskUser(curCustomer);
        }

        /// <summary>
        /// 获取当前用户的角色列表
        /// </summary>
        /// <param name=""></param>
        /// <param name=""></param>
        /// <returns></returns>
        public static IList<CustomerRole> GetCustomerRoles(this Customer customer, ICustomerService customerService = null)
        {
            if (customer == null) return null;
            if (customerService == null)
                customerService = EngineContext.Current.Resolve<ICustomerService>();
            return customerService.GetCustomerRoles(customer, false);
        }
        /// <summary>
        /// 判断用户是否有登录后台权限
        /// 区分前端会员用户及后台管理用户
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="customerService"></param>
        /// <returns></returns>
        public static bool IsAdminUser(this Customer customer, ICustomerService customerService = null)
        {
            var customerRoles = customer?.GetCustomerRoles(customerService) ?? null;
            if (customerRoles == null) return false;
            return customerRoles.Any(role => role.RoleType == RoleType.Administrator || role.RoleType == RoleType.BackendUser);
        }

        /// <summary>
        /// 判断用户是否有会员登录权限
        /// 区分前端会员用户及后台管理用户
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="customerService"></param>
        /// <returns></returns>
        public static bool IsGuestUser(this Customer customer, ICustomerService customerService = null)
        {
            var customerRoles = customer?.GetCustomerRoles(customerService) ?? null;
            if (customerRoles == null) return false;
            return customerRoles.Any(role => role.RoleType == RoleType.Guests);
        }
    }
}
